package juc.ThreadLocal;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;



/**
 * @author 况祥彬
 * @date 2020/12/17 10:05
 **/
public class MyThreadLocal {
}
@Slf4j
class Demo1 {
    static ThreadLocal<String> myThreadLocal = new ThreadLocal();
    static int count = 0;
    public static void main(String[] args) {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS,
                new LinkedBlockingDeque<>()
        );
        for (int i = 0; i < 2; i++) {
            int j = i;
            new Thread(() -> {
                if (j == 0) {
                    myThreadLocal.set("kk");
                } else {
                    myThreadLocal.set("wy");
                }
                MyPrint();
            }, "t" + j).start();
        }

    }

    private static void MyPrint() {
        log.debug("{}", myThreadLocal.get());
        myThreadLocal.remove();
        log.debug("remove after:{}", myThreadLocal.get());
    }
}

/**
 * 测试ThreadLocal的不可继承性
 */
@Slf4j
class Demo2{
    static InheritableThreadLocal<String> myThreadLocal2=new InheritableThreadLocal();

    public static void main(String[] args) {
        myThreadLocal2.set("kk");
        new Thread(()->{
            log.debug("t1:{}",myThreadLocal2.get());
        },"t1").start();

        log.debug("main:{}",myThreadLocal2.get());
    }
}